﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DALHelper.Business;

namespace DALHelper.DAL
{
    public class TransDetailDAL
    {
        private SqlConnection m_connection;

        public TransDetailDAL(SqlConnection conn)
        {
            m_connection = conn;
        }

        public void addTransDetail(string GroupCode, string strDetail, int wait)
        {
            try
            {
                m_connection.Open();

                SqlCommand cmd = new SqlCommand("sp_ThemChiTietGiaoDich", m_connection);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter para;

                para = new SqlParameter("MaDoan", System.Data.SqlDbType.NVarChar);
                para.Direction = ParameterDirection.Input;
                para.Value = GroupCode;
                cmd.Parameters.Add(para);

                para = new SqlParameter("ChuoiChiTiet", System.Data.SqlDbType.NVarChar);
                para.Direction = ParameterDirection.Input;
                para.Value = strDetail;
                cmd.Parameters.Add(para);

                para = new SqlParameter("Wait", System.Data.SqlDbType.Bit);
                para.Direction = ParameterDirection.Input;
                para.Value = wait;
                cmd.Parameters.Add(para);

                cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                m_connection.Close();
            }
        }

        public void UpdateTransDetail(IEnumerable<TransDetail> arrDetails)
        {
            try
            {
                m_connection.Open();

                foreach (TransDetail d in arrDetails)
                {
                    SqlCommand cmd = new SqlCommand("sp_CapNhatChiTietGiaoDich", m_connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter para;

                    para = new SqlParameter("MaDoan", System.Data.SqlDbType.NVarChar);
                    para.Direction = ParameterDirection.Input;
                    para.Value = d.TransactionID;
                    cmd.Parameters.Add(para);

                    para = new SqlParameter("MaChiTiet", System.Data.SqlDbType.NVarChar);
                    para.Direction = ParameterDirection.Input;
                    para.Value = d.TransDetailID;
                    cmd.Parameters.Add(para);

                    para = new SqlParameter("CaNhan", System.Data.SqlDbType.NVarChar);
                    para.Direction = ParameterDirection.Input;
                    para.Value = d.GuestName;
                    cmd.Parameters.Add(para);

                    para = new SqlParameter("CMND", System.Data.SqlDbType.NVarChar);
                    para.Direction = ParameterDirection.Input;
                    para.Value = d.GuestIDNumber;
                    cmd.Parameters.Add(para);

                    para = new SqlParameter("BatDau", System.Data.SqlDbType.DateTime);
                    para.Direction = ParameterDirection.Input;
                    para.Value = d.StartTime;
                    cmd.Parameters.Add(para);

                    para = new SqlParameter("KetThuc", System.Data.SqlDbType.DateTime);
                    para.Direction = ParameterDirection.Input;
                    para.Value = d.EndTime;
                    cmd.Parameters.Add(para);

                    para = new SqlParameter("MaPhong", System.Data.SqlDbType.NVarChar);
                    para.Direction = ParameterDirection.Input;
                    para.Value = d.RoomID;
                    cmd.Parameters.Add(para);

                    para = new SqlParameter("Wait", System.Data.SqlDbType.Bit);
                    para.Direction = ParameterDirection.Input;
                    para.Value = 0;
                    cmd.Parameters.Add(para);

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                m_connection.Close();
            }
        }

        //public void updateTransDetail(string strGroupCode, string strDetailCode, string strPerson, string strNumber, DateTime dtStart, DateTime dtEnd, string strRoomCode,int wait)
        //{
        //    try
        //    {
        //        m_connection.Open();

        //        SqlCommand cmd = new SqlCommand("sp_CapNhatChiTietGiaoDich", m_connection);
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        SqlParameter para;

        //        para = new SqlParameter("MaDoan", System.Data.SqlDbType.NVarChar);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = strGroupCode;
        //        cmd.Parameters.Add(para);

        //        para = new SqlParameter("MaChiTiet", System.Data.SqlDbType.NVarChar);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = strDetailCode;
        //        cmd.Parameters.Add(para);

        //        para = new SqlParameter("CaNhan", System.Data.SqlDbType.NVarChar);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = strPerson;
        //        cmd.Parameters.Add(para);

        //        para = new SqlParameter("CMND", System.Data.SqlDbType.NVarChar);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = strNumber;
        //        cmd.Parameters.Add(para);

        //        para = new SqlParameter("BatDau", System.Data.SqlDbType.DateTime);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = dtStart;
        //        cmd.Parameters.Add(para);

        //        para = new SqlParameter("KetThuc", System.Data.SqlDbType.DateTime);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = dtEnd;
        //        cmd.Parameters.Add(para);

        //        para = new SqlParameter("MaPhong", System.Data.SqlDbType.NVarChar);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = strRoomCode;
        //        cmd.Parameters.Add(para);

        //        para = new SqlParameter("Wait", System.Data.SqlDbType.Bit);
        //        para.Direction = ParameterDirection.Input;
        //        para.Value = wait;
        //        cmd.Parameters.Add(para);

        //        cmd.ExecuteNonQuery();
        //    }
        //    catch (SqlException e)
        //    {
        //        throw e;
        //    }
        //    finally
        //    {
        //        m_connection.Close();
        //    }
        //}

        public void delTransDetail(string GroupCode, string strDetail, int wait)
        {
            try
            {
                m_connection.Open();

                SqlCommand cmd = new SqlCommand("sp_XoaChiTietGiaoDich", m_connection);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter para;

                para = new SqlParameter("MaDoan", System.Data.SqlDbType.NVarChar);
                para.Direction = ParameterDirection.Input;
                para.Value = GroupCode;
                cmd.Parameters.Add(para);

                para = new SqlParameter("ChuoiChiTiet", System.Data.SqlDbType.NVarChar);
                para.Direction = ParameterDirection.Input;
                para.Value = strDetail;
                cmd.Parameters.Add(para);

                para = new SqlParameter("Wait", System.Data.SqlDbType.Bit);
                para.Direction = ParameterDirection.Input;
                para.Value = wait;
                cmd.Parameters.Add(para);

                cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                m_connection.Close();
            }
        }

        public void bookRooms(string strGroupCode, string strDetail, int wait)
        {
            try
            {
                m_connection.Open();
                SqlCommand cmd = new SqlCommand("sp_DatPhong", m_connection);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter para;

                para = new SqlParameter("MaDoan", System.Data.SqlDbType.VarChar);
                para.Direction = ParameterDirection.Input;
                para.Value = strGroupCode;
                cmd.Parameters.Add(para);

                para = new SqlParameter("ChuoiChiTiet", System.Data.SqlDbType.NVarChar);
                para.Direction = ParameterDirection.Input;
                para.Value = strDetail;
                cmd.Parameters.Add(para);

                para = new SqlParameter("Wait", System.Data.SqlDbType.Bit);
                para.Direction = ParameterDirection.Input;
                para.Value = wait;
                cmd.Parameters.Add(para);

                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                m_connection.Close();
            }
        }
    }
}
